package spring_annotation.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class LogAspects {

    //    @Pointcut("execution(public int spring_annotation.aop.MathCalculator.div(int,int))")
    @Pointcut(value = "execution(* spring_annotation.aop.MathCalculator.*(..)) && args(a, b)", argNames = "a, b")
    public void pointCut(int a, int b) {
    }

    //    @Before(value = "execution(* spring_annotation.aop.MathCalculator.*(..)) && args(a, b)", argNames = "a, b")
    @Before("pointCut(a, b)")
    public void logStart(JoinPoint joinPoint, int a, int b) {
        System.out.println(joinPoint.getSignature().getName() + "运行。。。参数列表是:[" + a + "," + b + "]");
    }

    @After("pointCut(a, b)")
    public void logEnd(JoinPoint joinPoint, int a, int b) {
        System.out.println(joinPoint.getSignature().getName() + "结束。。。");
    }

    //    @AfterReturning(value = "spring_annotation.aop.LogAspects.pointCut()", returning = "result")
    @AfterReturning(value = "pointCut(a, b)", returning = "result")
    public void logReturn(JoinPoint joinPoint, Object result, int a, int b) {
        System.out.println(joinPoint.getSignature().getName() +
                ":正常返回。。。运行结果:" + a + "/" + b + "= [" + result + "]");
    }

    @AfterThrowing(value = "pointCut(a, b)", throwing = "ex")
    public void logException(JoinPoint joinPoint, Exception ex, int a, int b) {
        System.out.println(joinPoint.getSignature().getName() +
                ":异常。。。异常信息:[" + ex + "]");
    }
}
